

const isTrue = (num) => {
    const n = num.toString().length
    const str = num.toString()
    let sum = 0
    for (let i = 0; i < n; i++) {
        sum += str[i] ** n
    }
    return sum === num
}


const getNum = (n) => {
    let str = ''
    let res = []

    function dfs(index) {
        if (str.length === n && isTrue(Number(str))) {
            res.push(Number(str))
            return
        }
        for (let i = 1; i <= 9; i++) {
            str += i
            dfs(1)
            str = str.slice(0, -1)
        }
    }

    dfs(1)
    return res.sort((a, b) => a - b)
}

console.log(getNum(4));
